//
//  IMINetTwitterEngine.m
//  IMINet
//
//  Created by Travis on 09-11-29.
//  Copyright imi.im 2009. All rights reserved.
//	More Information: http://imi.im

#import "IMINetTwitterEngine.h"

#define TWITTER_URL_API		@"twitter.com"
#define TWITTER_URL_FRIENDS	@"friends.xml"
#define TWITTER_URL_FRIENDS_UPDATES	@"friends_timeline.xml"

@implementation IMINetTwitterEngine

- (id) init
{
	self = [super init];
	if (self != nil) {
		self.useSSL=YES;
		self.apiURL=TWITTER_URL_API;
		
	}
	return self;
}

-(NSString*)connectWithRequeset:(IMINetRESTRequest*)req{
	IMINetConnection *nc=[[IMINetConnection alloc] initWithRequest:req delegate:self];
	nc.username=self.username;
	nc.password=self.password;
	return nc.uuid;
}
-(NSString*)generateReqURLWithSubfix:(NSString*)sf{
	NSString *url;
	if (useSSL) {
		url=@"https://";
	}else {
		url=@"http://";
	}
	url=[url stringByAppendingFormat:@"%@/statuses/%@",self.apiURL,TWITTER_URL_FRIENDS];
	return url;
}




-(NSString*)getFriends{
	NSString *url=[self generateReqURLWithSubfix:TWITTER_URL_FRIENDS];
	
	IMINetRESTRequest *nr=[IMINetRESTRequest requestWithURL:[NSURL URLWithString:url]];
	return [self connectWithRequeset:nr];
}

-(NSString*)getFriendsUpdate{
	NSString *url=[self generateReqURLWithSubfix:TWITTER_URL_FRIENDS_UPDATES];
	
	IMINetRESTRequest *nr=[IMINetRESTRequest requestWithURL:[NSURL URLWithString:url]];
	return [self connectWithRequeset:nr];
}


-(void)connection:(IMINetConnection *)connection successWithContentLength:(NSInteger)clength{
	NSLog(@"%@ success: %i",connection.uuid,clength);
	
}
-(void)connection:(IMINetConnection *)connection failWithString:(NSString*)s{
	NSLog(@"%@ fail: %@",connection.uuid,s);
}
-(void)connectionDidReceiveData:(IMINetConnection *)connection{
	//NSLog(@"%@ load: %i/%i",connection.uuid,connection.loadedBytes,connection.totalBytes);
	//NSData *data=connection.response.data;
	//UIImage *img=[UIImage imageWithData:data];
	//	
	//	[iv setImage:img];
	//	[iv setFrame:CGRectMake(0, 0, 320, img.size.width*320/img.size.height)];
	
}
-(void)connectionDidFinishLoading:(IMINetConnection *)connection{
	NSData *data=connection.response.data;
	NSLog(@"%@ Finish %@",connection.uuid,[NSString stringWithUTF8String:[data bytes]]);
	
	[[IMINetXML alloc] initWithData:connection.response.data withDelegate:self];
}
-(void)XMLDidFinish:(IMINetXML*)xml{
	
	[xml release];
}
@end
